package com.manager.devicemanager.controller;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.manager.devicemanager.common.Result;
import com.manager.devicemanager.config.FileUploadConfig;
import com.manager.devicemanager.config.ServiceException;
import com.manager.devicemanager.entity.TClientSystemFile;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.util.StringUtils;
import com.manager.devicemanager.service.ITClientSystemFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import com.manager.devicemanager.vo.PageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.io.File;
import java.util.List;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wpf
 * @since 2025-08-02
 */
@Tag(name = "TClientSystemFileController", description = " 相关接口")
@RestController
@RequestMapping("/tClientSystemFile")
public class TClientSystemFileController {
    @Autowired
    private FileUploadConfig fileUploadConfig;

    @Autowired
    private ITClientSystemFileService iTClientSystemFileService;

    @Operation(summary = "新增")
    @PostMapping("/save")
    public Result<TClientSystemFile> save(@Parameter(description = "TClientSystemFile 对象") @RequestBody TClientSystemFile tClientSystemFile){
       iTClientSystemFileService.save(tClientSystemFile);
        return Result.success(tClientSystemFile);
    }

    @Operation(summary = "按id删除")
    @PostMapping("/delete/{id}")
    public Result<String> delete(@Parameter(description = "记录ID", example = "1") @PathVariable("id") String id){
        TClientSystemFile tClientSystemFile = iTClientSystemFileService.getById(id);
        if (tClientSystemFile != null) {
            File saveFile = new File(fileUploadConfig.getUploadDir(), tClientSystemFile.getFilepath());
            if (saveFile.exists()) {
                saveFile.delete();
            }
            iTClientSystemFileService.removeById(id);
            return Result.success(id);
        } else {
            return Result.error("记录不存在");
        }
    }

    @Operation(summary = "按id修改")
    @PostMapping("/update/{id}")
    public Result<TClientSystemFile> update(@Parameter(description = "记录ID", example = "1") @PathVariable("id") String id, @Parameter(description = "TClientSystemFile 对象") @RequestBody TClientSystemFile tClientSystemFile){
        tClientSystemFile.setId(id);
       iTClientSystemFileService.updateById(tClientSystemFile);
        return Result.success(tClientSystemFile);
    }

    @Operation(summary = "按id查询")
    @GetMapping("/get/{id}")
    public Result<TClientSystemFile> get(@Parameter(description = "记录ID", example = "1") @PathVariable("id") String id){
        TClientSystemFile tClientSystemFile =iTClientSystemFileService.getById(id);
        return Result.success(tClientSystemFile);
    }

    @Operation(summary = "条件查询带分页")
    @PostMapping("/page")
    public Result<IPage<TClientSystemFile>> page(@Parameter(description = "查询条件对象") @RequestBody  PageVO<TClientSystemFile> tClientSystemFileQuery){
        int pageNum = tClientSystemFileQuery.getPageNum();
        int pageSize = tClientSystemFileQuery.getPageSize();
        TClientSystemFile tClientSystemFile = tClientSystemFileQuery.getCondition();
         LambdaQueryWrapper<TClientSystemFile> wrapper =new LambdaQueryWrapper<>(tClientSystemFile);
         wrapper.orderByDesc(TClientSystemFile::getVersionnum);
        //if (StringUtils.hasText(tClientSystemFile.getName())) {
          //  wrapper.like("name", tClientSystemFile.getName());
        //}
        IPage<TClientSystemFile> tClientSystemFileList =iTClientSystemFileService.page(new Page<>(pageNum, pageSize),wrapper );
        return Result.success(tClientSystemFileList);
    }

    @Operation(summary = "条件查询")
    @PostMapping("/list")
    public Result<List<TClientSystemFile>> list(@Parameter(description = "TClientSystemFile 查询条件对象") @RequestBody TClientSystemFile tClientSystemFile){
        List<TClientSystemFile> tClientSystemFileList =iTClientSystemFileService.list(new QueryWrapper<>(tClientSystemFile));
        return Result.success(tClientSystemFileList);
    }

    @Operation(summary = "升级")
    @PostMapping("/upgrade/{id}")
    public void upgrade(@PathVariable("id") String id,@RequestParam("devicetopic") String devicetopic) {
        try {
            iTClientSystemFileService.upgrade(id,devicetopic);
        } catch (Exception e) {
            e.printStackTrace();
            throw  new ServiceException("异常"+e.getMessage());
        }
//        iTClientSystemFileService.upgrade(id);
    }
}
